<template>
  <div class="login-account">
    <el-form label-width="70px" :model="account" :rules="rules" ref="formRef">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { rules } from '../config/account-config'
import cache from '@/utils/cache'
import { ElForm } from 'element-plus'
export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      name: cache.getCache('account')?.name ?? '',
      password: cache.getCache('account')?.password ?? ''
    })
    //通过<InstanceType<typeof 组件实例>>获取组件的类型
    const formRef = ref<InstanceType<typeof ElForm>>()
    const clickLogin = (isRememberPswd: boolean) => {
      console.log('点击了登录按钮')
      formRef.value?.validate((vali) => {
        if (vali) {
          console.log('验证通过了')
          if (isRememberPswd) {
            cache.setCache('account', account)
          } else {
            cache.deleteCache('account')
          }
          store.dispatch('login/accountLoginAction', { ...account })
        } else {
          console.log('验证失败')
        }
      })
    }

    return {
      account,
      rules,
      clickLogin,
      formRef
    }
  }
})
</script>

<style scoped></style>
